﻿using System;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.Handler;
using Timer = System.Threading.Timer;
using Slippor.Common.PluginFramework;
using System.ComponentModel;
using RestoreHandlerPlugin.Properties;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace RestoreHandlerPlugin
{
    public class RestoreHandler : IHandler
    {
        public const string Key = "AutoRestore";

        private Song _currentSong = Song.DefaultSong;
        private IECacheFileFetcher _cacheFetcher = new IECacheFileFetcher();

        public RestoreHandler()
        {
            ;
        }

        public string Name
        {
            get { return Key; }
        }

        public void Hand(Song song)
        {
            ;
        }

        private Settings settings = RestoreConfig.SettingManager.Settings as Settings;

        private String nameFormate { get { return settings.NameFormate; } }
        private String path
        {
            get
            {
                if (settings.Path.IndexOf(':') == -1)
                {
                    return Path.GetDirectoryName(Application.ExecutablePath) + "\\" + settings.Path;
                }
                return settings.Path;
            }
        }

        public void Drop(Song song)
        {
            if (song.FaveState == FavState.Fav || !settings.OnlyFav)
            {
                using (BackgroundWorker copyWorker = new BackgroundWorker())
                {
                    copyWorker.DoWork += (sender, e) =>
                        {
                            if (!String.IsNullOrEmpty(song.SongID))
                            {
                                String songPath = _cacheFetcher.CopySong(song, path, nameFormate, settings.OnlyFull, (DirOptions)settings.DirOptions);

                                if (settings.SaveCover && song.Album != null && !String.IsNullOrEmpty(song.Album.CoverID))
                                {
                                    if(settings.EmbeddedCover)
                                    {
                                        if(!String.IsNullOrEmpty(songPath))
                                        {
                                            try
                                            {
                                                _cacheFetcher.AttachCover(song.Album, songPath);
                                            }
                                            catch (Exception ex)
                                            {
                                                Trace.TraceError(ex.Message);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        _cacheFetcher.CopyCover(song.Album, path, (DirOptions)settings.DirOptions);
                                    }
                                }
                            }
                        };
                    copyWorker.RunWorkerAsync();
                }
            }
        }
    }
}